<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Login</title>
  <style>
    body {
      background-color: #f2f2f2;
      font-family: Arial, sans-serif;
      font-size: 16px;
      line-height: 1.5;
      color: #333;
    }
    .container {
      max-width: 500px;
      margin: 0 auto;
      padding: 20px;
      background-color: #fff;
      border-radius: 5px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }
    h1 {
      text-align: center;
      margin-top: 0;
      margin-bottom: 20px;
    }
    label {
      display: block;
      margin-bottom: 10px;
      font-weight: bold;
    }
    input[type="text"], input[type="password"] {
      display: block;
      width: 100%;
      padding: 10px;
      border: 2px solid #ccc;
      border-radius: 5px;
      font-size: 16px;
      margin-bottom: 20px;
    }
    button {
      display: block;
      width: 100%;
      padding: 10px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 5px;
      font-size: 16px;
      cursor: pointer;
    }
    button:hover {
      background-color: #0069d9;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Bank Login</h1>
    <form>
      <label for="cardNumber">card number:</label>
      <input type="text" id="cardNumber" name="cardNumber" placeholder="Please enter your bank card number" required>
      <label for="password">password:</label>
      <input type="password" id="password" name="password" placeholder="Please enter your password" required>
      <button type="submit">login</button>
    </form>
  </div>
  
  <script>
    const form = document.querySelector('form');
    form.addEventListener('submit', (event) => {
      event.preventDefault();
      const cardNumber = document.querySelector('#cardNumber').value;
      const password = document.querySelector('#password').value;
      console.log(`银行卡号：${cardNumber}，密码：${password}`);


      // TODO: 前端输入验证（卡号密码是否符合规范）

      // 调用后端接口进行登录验证
      var xhr = new XMLHttpRequest();
      xhr.open("POST", "/login", true);
      xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4 && xhr.status == 200) {
            var response = JSON.parse(xhr.response);
            // 登陆成功
            if (response.status == 0) {
              // TODO: 使用localStorage仅供在本地浏览器测试用，真实环境中可能会有问题，可修改
              localStorage.setItem("token", response.token)
              location.href = './index.html'
            }
            else{
              window.confirm("Card number or password error!")
            }
        }
      };
      xhr.send("username=" + cardNumber +"&password=" + password);
      return false;

    });
  </script>
</body>
</html>